package com.lagou.edu.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;


@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    @RequestMapping("/findAll")
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        response.setCharacterEncoding("UTF-8");
        List<Resume> resumes = resumeService.findAll();

        HttpSession session = request.getSession();
        session.setAttribute("resumeList", resumes);
        session.setAttribute("resumeSize", resumes.size());
        request.setAttribute("resumeList", resumes);
        request.setAttribute("resumeSize",resumes.size());

        String resData = JSON.toJSONString(resumes);

        System.out.println("resume findAll");
        System.out.println(resData);

        PrintWriter pw = response.getWriter();
        pw.write(resData);
        pw.close();

    }

    @RequestMapping("/save")
    public void save(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        response.setCharacterEncoding("UTF-8");
        Resume resume = new Resume();
        Long id = null;
        try {
            id = Long.parseLong(request.getParameter("id"));
            resume.setId(id);
        }catch (Exception e){
        }
        try {
            String name = request.getParameter("name");
            String phone = request.getParameter("phone");
            String address = request.getParameter("address");

            resume.setName(name);
            resume.setPhone(phone);
            resume.setAddress(address);

            Resume saveResume = resumeService.save(resume);

            String resData = JSON.toJSONString("success");

            PrintWriter pw = response.getWriter();
            pw.write(resData);
            pw.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @RequestMapping("/delete")
    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        response.setCharacterEncoding("UTF-8");

        System.out.println(request.getParameter("id"));
        String resData = JSON.toJSONString("success");
        Long id = null;
        try {
            id = Long.parseLong(request.getParameter("id"));
        }catch (Exception e){
        }

        if (id != null && id>0){
            boolean delete = resumeService.deleteById(id);
            resData = JSON.toJSONString("删除成功");
        }else {
            resData = JSON.toJSONString("记录不存在不存在");
        }

        PrintWriter pw = response.getWriter();
        pw.write(resData);
        pw.close();

    }
}
